package com.source.lightfowl.barcodeMachineGrantAndRecovery.service.impl;

import com.source.lightfowl.VO.BarcodeMachineGrantVO;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.dao.BarcodeMachineGrantVersion2Dao;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.pojo.BarcodeMachineGrant;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.service.BarcodeMachineGrantService;
import com.source.lightfowl.basedata.dao.StallsDao;
import com.source.lightfowl.basedata.entity.Stalls;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: xcy
 * @Date:Create：in 2020/3/16 22:12
 * @Description: 脚标机发放管理 业务层实现类
 */
@Service
public class BarcodeMachineGrantServiceImpl implements BarcodeMachineGrantService {

    //注入持久层接口
    @Autowired
    private BarcodeMachineGrantVersion2Dao barcodeMachineGrantDao;
    @Autowired
    private PersonDao personDao;
    @Autowired
    private StallsDao stallsDao;
    /*
     查找全部
     */
    @Override
    public List<BarcodeMachineGrantVO> findAll() {
        List<BarcodeMachineGrant> list = barcodeMachineGrantDao.findAll();
        List<BarcodeMachineGrantVO> collect = list.stream().map(s -> {
            BarcodeMachineGrantVO vo = new BarcodeMachineGrantVO();
            BeanUtils.copyProperties(s,vo);

            //查找发放人资料
            Person user = personDao.findVO(s.getGrantPersonId());
            //发放人姓名
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
            }

            Person user1 = personDao.findVO(s.getReceivePersonId());
            //姓名
            if (user1 != null) {
                vo.setRecyclingName(user1.getLoginName());
            }

            Person user3 = personDao.findVO(s.getCreateBy());
            if (user3 != null) {
                vo.setCreateName(user3.getLoginName());
            }
            Optional<Stalls> optional = stallsDao.findById(Integer.valueOf(s.getStallsId()));
            if (optional != null) {
                vo.setStallsName(optional.get().getStallsName());
            }


            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    /*
    分页查找
     */
    @Override
    public Page<BarcodeMachineGrant> findAllPage(int page, int pageNum) {

        return barcodeMachineGrantDao.findAll(PageRequest.of(page-1,pageNum));
    }

    /*
    根据脚标机id查找
     */
    @Override
    public List<BarcodeMachineGrant> findByBarcodeMachineId(Integer id) {
        return barcodeMachineGrantDao.findByBarcodeMachineId(id);

    }

    /*
    根据id查找
     */
    @Override
    public BarcodeMachineGrantVO findById(Integer id) {
        //todo
        Optional<BarcodeMachineGrant> optional = barcodeMachineGrantDao.findById(id);
        if (optional.isPresent()){
            BarcodeMachineGrant s = optional.get();
            BarcodeMachineGrantVO vo = new BarcodeMachineGrantVO();
            BeanUtils.copyProperties(s,vo);
            Person user = personDao.findVO(s.getGrantPersonId());
            //发放人姓名
            if (user != null) {
                vo.setDeliverName(user.getLoginName());
            }

            Person user1 = personDao.findVO(s.getReceivePersonId());
            //发放人姓名
            if (user1 != null) {
                vo.setRecyclingName(user1.getLoginName());
            }
            Person user3 = personDao.findVO(s.getCreateBy());
            if (user3 != null) {
                vo.setCreateName(user3.getLoginName());
            }
            Optional<Stalls> optional2 = stallsDao.findById(Integer.valueOf(s.getStallsId()));
            if (optional != null) {
                vo.setStallsName(optional2.get().getStallsName());
            }


            return vo;
        }

        return null;
    }

    /*
    增加/修改 数据
     */
    @Override
    public void save(BarcodeMachineGrant barcodeMachineGrant) {
        //更新创建时间
        barcodeMachineGrant.setCreateTime(LocalDateTime.now());
        barcodeMachineGrantDao.save(barcodeMachineGrant);
    }

    /*
    根据id删除
     */
    @Override
    public void deleteById(Integer id) {
        barcodeMachineGrantDao.deleteById(id);
    }

    /*
    批量删除
     */
    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        if(list != null) {
            barcodeMachineGrantDao.deleteBatch(list);
        }else {
            System.out.println("批量删除传递参数出错");
            throw new RuntimeException("传递的id参数格式出错，id之间需要用英文逗号分隔开，以字符串格式传递");
        }
    }
}
